<template>
	<el-row >
		<el-col span="8" style="width:600px;" class="warp-main">
				<el-form ref="form"  :model="form" :rules="rules" status-icon label-width="120px"   class="demo-ruleForm">
				<el-form-item label="原密码" prop='telpow'>
					<el-input v-model="form.telpow"></el-input>
				</el-form-item>
				<el-form-item label="新密码" prop='newPwd'>
					<el-input v-model="form.newPwd"></el-input>
				</el-form-item>
				<el-form-item label="确认新密码" prop='confirmPwd'>
					<el-input v-model="form.confirmPwd"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleChangepwd">提交</el-button>
				</el-form-item>
			</el-form>
		</el-col>
	</el-row>
</template>
<script>
	 import { modifyPassword } from '../../api/api.js';
	 // import { modifyPassword } from '../../../api/apiGetPost.js';
export default{
	data(){
		var checkAge = (rule, value, callback) => {
			if (value != this.form.checkPass) {
				return callback(new Error('原密码不正确'));
			}else {
				callback();
			}
		};
		var validatePass = (rule, value, callback) => {
			if (value === '') {
				callback(new Error('请输入新密码'));
			} else {
				if (this.form.confirmPwd !== '') {
					this.$refs.form.validateField('confirmPwd');
				}
				callback();
			}
		};
		var validatePass2 = (rule, value, callback) => {
			if (value === '') {
				callback(new Error('请再次输入密码'));
			} else if (value !== this.form.newPwd) {
				callback(new Error('两次输入密码不一致!'));
			} else {
				callback();
			}
		};
		return {
			rules: {
				newPwd: [
					{ required: true, message: '请输入新密码', trigger: 'blur' },
					{ min: 6, max: 15, message: '长度在 6 到15 个字符', trigger: 'blur' },
					{ validator: validatePass, trigger: 'blur' },
					
				],
				confirmPwd: [
					{ validator: validatePass2, trigger: 'blur' },
					
				],
				telpow: [
					{ required: true, message: '请输入旧密码', trigger: 'blur' },
					{ min: 6, max: 15, message: '长度在 6 到15 个字符', trigger: 'blur' },
					{ validator: checkAge, trigger: 'blur' },
				]
			},
			form:{
				telpow:'',
				newPwd:'',
				confirmPwd:'',
				checkPass:'123456',
				account:'',
			}
		}
	},
	methods: {
		handleChangepwd() {
			this.$refs.form.validate((valid) => {
				if (valid) {
					let para = Object.assign({}, this.form);
					console.log(para);
					modifyPassword(para).then((res) => {
						console.log(res);
						if(res.successful){
							this.$message({
								message: '修改成功',
								type: 'success'
							});
							this.$refs['form'].resetFields();
							this.$router.push({ path: '/Login' });
						}else{
							this.$message({
								message: '修改失败',
								type: 'warning'
							});
						}
					});
				} else {
					console.log('error submit!!');
					return false;
				}
			});
		}
	},
	mounted() {
	}
}
</script>
<style scoped>
	.warp{
		width: 500px;
	}
</style>
